package br.com.iplant.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import br.com.iplant.util.Conexao;
import br.com.iplant.vo.Doenca;


public class DoencaDAO {
	public String salvar(Doenca doenca) {
		try {

			String sql = "insert into doencas(descricao) " + "values (?)";
			PreparedStatement ps = Conexao.getCon().prepareStatement(sql);

			ps.setString(1, doenca.getDescricao());

			ps.execute();

			System.out.println("Gravado!");
			ps.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "" + doenca;
	}

	public List<Doenca> getTodasAsDoencas() {
		List<Doenca> doencas = new ArrayList<Doenca>();
		try {
			String sql = "select * from doencas";
			Statement ps = Conexao.getCon().createStatement();
			ResultSet rs = ps.executeQuery(sql);
			while (rs.next()) {
				doencas.add(new Doenca(rs.getInt("doenca_id"), rs
						.getString("descricao"), rs.getString("nomeCientifico"), rs.getString("sintomas"), rs.getString("controle")));
			}

			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return doencas;
	}
}
